package com.test.chat.topic;

import java.util.LinkedList;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class TopicManager {
	
	private Map<String, LinkedList<String>> topicStore = new ConcurrentHashMap<>();
	
	public void add(String topic, String username) {
		if(!topicStore.containsKey(topic)) {
			try {
				createTopic(topic, topicStore);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		LinkedList<String> usernames = topicStore.get(topic);
		topicStore.put(topic, usernames);
	}

	public void remove(String topic, String username){
		LinkedList<String> usernames = topicStore.get(topic);
		usernames.remove(topic);
	}
	
	private synchronized void createTopic(String topic, Map<String, LinkedList<String>> topicStore) throws Exception {
		if(topicStore.containsKey(topic)){
			throw new Exception("已经存在topic" + topic);
		}
		LinkedList<String> usernames = new LinkedList<>();
		topicStore.put(topic, usernames);
	}
	

}
